//
//  ViewController.m
//  Inteinational国际
//
//  Created by dlios on 15/6/30.
//  Copyright (c) 2015年 mogu.com 蘑菇科技. All rights reserved.
//

#import "ViewController.h"
#import "International.h"

@interface ViewController ()

@property (strong, nonatomic) IBOutlet UILabel *myLabel;

@property (strong, nonatomic) IBOutlet UIButton *myButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIView *viewA = [[UIView alloc]initWithFrame:CGRectMake(8, 8, 8, 8)];
    viewA.backgroundColor = [ UIColor blackColor];
    [self.view addSubview:viewA];
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    [center addObserver:self selector:@selector(setChangeLanguage) name:@"change" object:nil];
    
    // 初始化语言
    [International initUserLanguage];
    
    // 获取路径
    NSBundle *bundle = [International bundle];
    
    // 先获取language.strings文件的数据, 获取label的值
    NSString *inviteMsg = [bundle localizedStringForKey:@"myLabel" value:nil table:@"language"];
    
    // 获取Button的值
    NSString *buttonInfo = [bundle localizedStringForKey:@"myButton" value:nil table:@"language"];
    
    _myLabel.text = inviteMsg;
    [_myButton setTitle:buttonInfo forState:UIControlStateNormal];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)myButton:(id)sende
{
    // 获取当前设备的语言环境
    NSString *language = [International userLanguage];
    
    if ([language isEqualToString:@"en"]) {
        
        [International setUserlanguage:@"zh-Hans"];
        
    } else {
        
        [International setUserlanguage:@"en"];
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"change" object:nil];
}


- (void)setChangeLanguage
{
    NSBundle *bundle = [International bundle];
    
    NSString *buttonTitle = [bundle localizedStringForKey:@"myButton" value:nil table:@"language"];
    
    [_myButton setTitle:buttonTitle forState:UIControlStateNormal];
    
    
    NSString *labelText = [bundle localizedStringForKey:@"myLabel" value:nil table:@"language"];
    
    _myLabel.text = labelText;
    NSLog(@"maomao");
}

@end


